<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePermission extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if(!Schema::hasTable('users')) {
            Schema::create('users', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->string('password', 60);
                $table->rememberToken();
                $table->timestamps();
            });
        }

        if(!Schema::hasTable('roles')) {
            Schema::create('roles', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->timestamps();
                $table->string('name', 255);
                $table->string('label', 255);
                $table->string('description', 255);
            });
        }

        if(!Schema::hasTable('permissions')) {
            Schema::create('permissions', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->timestamps();
                $table->string('name', 255);
                $table->string('label', 255);
                $table->string('description', 255);
                $table->integer('cid');
            });
        }
        if(!Schema::hasTable('role_user')) {
            Schema::create('role_user', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->integer('user_id')->unsigned();
                $table->integer('role_id')->unsigned();
            });
        }
        Schema::table('role_user', function (Blueprint $table) {
            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade')
                ->onUpdate('restrict')
               ;
            $table->foreign('role_id')
                ->references('id')
                ->on('roles')
                ->onDelete('cascade')
                ->onUpdate('restrict')
               ;
        });

        if(!Schema::hasTable('permission_role')) {
            Schema::create('permission_role', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->integer('permission_id')->unsigned();;
                $table->integer('role_id')->unsigned();

            });
        }
        Schema::table('permission_role', function (Blueprint $table) {
            $table->foreign('permission_id')
                ->references('id')
                ->on('permissions')
                ->onDelete('cascade')
                ->onUpdate('restrict')
               ;
            $table->foreign('role_id')
                ->references('id')
                ->on('roles')
                ->onDelete('cascade')
                ->onUpdate('restrict')
               ;
        });

        if(!Schema::hasTable('password_resets')) {
            Schema::create('permission_role', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->timestamps();
                $table->string('email', 255);
                $table->string('token', 255);
            });
        }


    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('role_user');
        Schema::dropIfExists('permission_role');
        Schema::dropIfExists('password_resets');
        Schema::dropIfExists('user');
        Schema::dropIfExists('roles');
        Schema::dropIfExists('permissions');
        Schema::dropIfExists('users');
    }
}
